<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 130px;"
        class="filter-item"
        placeholder="请输入设备编号"
        v-model="queryForm.deviceCode"
      >
      </el-input>
      <el-input
        style="width: 120px;"
        class="filter-item"
        placeholder="请输入批次号"
        v-model="queryForm.batchNo"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.model"
        placeholder="请选择设备型号"
      >
        <el-option
          v-for="(val, key) in Dicts.DEVICE_HARDWARE_VER"
          :key="key"
          :label="val"
          :value="key"
        ></el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        @click="addDeviceStockBatch()"
        type="success"
        icon="el-icon-plus"
        v-permission="'deviceStock:add'"
        >新增</el-button
      >
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        @click="openFileDialog()"
        type="info"
        icon="el-icon-upload2"
        v-permission="'deviceStock:import'"
        >导入</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="model" label="设备型号" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.DEVICE_HARDWARE_VER[scope.row.model] }}
        </template>
      </el-table-column>
      <el-table-column prop="batchNo" label="批次号" width="250" align="center">
      </el-table-column>
      <el-table-column
        prop="deviceCode"
        label="设备编号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="vendor"
        label="生产厂商"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="produceDate"
        label="生产日期"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        label="设备状态"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.DEVICE_STOCK_STATUS[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="editDeviceStockBatch(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            v-permission="'deviceStock:update'"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
            v-permission="'deviceStock:delete'"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'新增设备库存'"
      :visible.sync="dialogAddFormVisible"
      width="30%"
    >
      <el-form
        :model="addForm"
        label-width="120px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-form-item label="批次号" prop="batchNo">
          <el-input
            :readonly="true"
            v-model="addForm.batchNo"
            auto-complete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="门禁设备编号" prop="deviceCode">
          <el-input
            v-model="addForm.deviceCode"
            auto-complete="off"
            maxlength="10"
            placeholder="请输入设备编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-select
            v-model="addForm.model"
            placeholder="请选择设备型号"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.DEVICE_HARDWARE_VER"
              :key="key"
              :label="val"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂商" prop="vendor">
          <el-input
            v-model="addForm.vendor"
            auto-complete="off"
            placeholder="请输入生产厂商"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="produceDate">
          <el-date-picker
            v-model="addForm.produceDate"
            value-format="yyyy-MM-dd hh:mm:ss"
            type="datetime"
            placeholder="请选择生产日期"
            style="width: 100%;"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execAddForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="'编辑设备库存'"
      :visible.sync="dialogEditFormVisible"
      width="30%"
    >
      <el-form
        :model="editForm"
        label-width="120px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input
            v-model="editForm.deviceCode"
            auto-complete="off"
            maxlength="0"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-select
            v-model="editForm.model"
            placeholder="请选择设备型号"
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.DEVICE_HARDWARE_VER"
              :key="key"
              :label="val"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execEditForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择文件" :visible.sync="fileVisible" width="30%">
      <el-form
        :model="fileForm"
        label-width="100px"
        :rules="fileFormRules"
        ref="fileForm"
      >
        <el-form-item label="文件" prop="file">
          <el-row>
            <el-col :span="12">
              <el-upload
                class="upload-demo"
                action=""
                :limit="1"
                ref="avatarInput"
                :on-change="handleChange"
                :auto-upload="false"
                accept="execl"
              >
                <el-button slot="trigger" size="small" type="primary"
                  >选取文件</el-button
                >
                <div slot="tip" class="el-upload__tip">
                  只能上传xls文件,替换文件请先移除已选中的文件
                </div>
              </el-upload>
            </el-col>
            <el-col :span="12">
              <el-button
                type="primary"
                size="small"
                onclick="location.href='http://keisdom.oss-cn-hangzhou.aliyuncs.com/templates/deviceStock_template_v1.0.xls'"
                >下载模板</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fileVisible = false">取 消</el-button>
        <el-button type="primary" @click="uploadFile">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryDeviceStock,
  queryDeviceStockBatch,
  addDeviceStockBatch,
  deleteDeviceStockBatch,
  uploadFile
} from '@/api/device'
export default {
  name: 'deviceStock',
  data() {
    return {
      fileVisible: false,
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        typeName: undefined,
        batchNo: undefined,
        status: undefined
      },
      addFormRules: {
        batchNo: [{ required: true, message: '请输入批次号', trigger: 'blur' }],
        deviceCode: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        model: [{ required: true, message: '请选择设备型号', trigger: 'blur' }],
        vendor: [
          { required: true, message: '请输入生产厂商', trigger: 'blur' }
        ],
        produceDate: [
          { required: true, message: '请选择生产日期', trigger: 'blur' }
        ]
      },
      addForm: {},
      editFormRules: {
        deviceCode: [
          { required: true, message: '请输入设备编号', trigger: 'blur' }
        ],
        model: [{ required: true, message: '请选择设备型号', trigger: 'blur' }]
      },
      editForm: {},
      fileForm: {},
      fileFormRules: {},
      typeOptions: Dicts.CONSUMER_STATUS,
      records: [],
      options: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleChange(file, fileList) {
      if (
        file.name.substring(file.name.lastIndexOf('.'), file.name.length) !==
        '.xls'
      ) {
        this.$message({
          showClose: true,
          message: '只能上传xls文件',
          type: 'error'
        })
        this.$refs.avatarInput.clearFiles()
        return
      }
      this.fileForm.param = file.raw
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryDeviceStock(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该批次, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDeviceStockBatch(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openAddDialog() {
      this.addForm = {}
      this.dialogAddFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    addDeviceStockBatch() {
      queryDeviceStockBatch().then(({ data: { code, msg, data } }) => {
        if (code === 0) {
          this.openAddDialog()
          this.addForm.batchNo = msg
        }
      })
    },
    execAddForm() {
      this.addForm.status = 0
      this.$refs.addForm.validate(valid => {
        if (valid) {
          addDeviceStockBatch(this.addForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogAddFormVisible = false
              }
              this.$message({
                showClose: true,
                message: '添加门禁设备库存成功',
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    openEditDialog() {
      this.editForm = {}
      this.dialogEditFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    editDeviceStockBatch(row) {
      this.openEditDialog()
      this.editForm = {
        rowId: row.rowId,
        status: row.status,
        batchNo: row.batchNo,
        deviceCode: row.deviceCode,
        vendor: row.vendor,
        produceDate: row.produceDate,
        model: row.model
      }
    },
    execEditForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          addDeviceStockBatch(this.editForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    },
    openFileDialog() {
      this.fileForm = {}
      this.fileVisible = true
      this.$refs.fileForm && this.$refs.fileForm.resetFields()
    },
    uploadFile() {
      var excle = new FormData()
      excle.append('file', this.fileForm.param)
      uploadFile(excle).then(({ data: { data, msg } }) => {
        this.fileVisible = false
        this.$refs.avatarInput.clearFiles()
        this.doQuery()
        this.$message({
          showClose: true,
          message: '导入成功',
          type: 'success'
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
